package com.example.eeuse;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.example.eeuse.util.KibanaUtil;

import java.nio.charset.Charset;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KibanaGet {
    public static void main(String[] args) {
        String date="2023-10-23";
        Map<String,Integer>  countMap= getSampleMap(date);
        //打印排序后的结果
        System.out.println("开始");
        countMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).forEach(entry -> System.out.println(entry.getKey()+":"+entry.getValue()));
        System.out.println("结束");
    }

    public static Map<String,Integer>  getSampleMap(String date)
    {
        //统计，key是航线，value是查询数量
        Map<String,Integer> countMap = new HashMap<>();
        //构建yyyy-MM-dd HH,因为我们的时区比es时区大8小时，所以要减去8小时
        for(int i=0;i<=23;i++){
            //得到最终的yyyy-MM-ddTHH
            DateTime dateTime = DateUtil.offsetHour(DateUtil.parse(date + " " + String.format("%02d", i) + ":00:00"), -8);
            String format = DateUtil.format(dateTime, "yyyy-MM-dd'T'HH");
            System.out.println(format);
            //创建post请求
            HttpRequest post = KibanaUtil.createPost("shopping_log_part_sample-");
            //设置请求体
            post.body("{\n" +
                    "  \"size\": 0,\n" +
                    "  \"query\": {\n" +
                    "    \"bool\": {\n" +
                    "      \"must\": [\n" +
                    "        {\n" +
                    "          \"match\": {\n" +
                    "            \"SAMPLE_NODE.keyword\": \"SAMPLE_RESPONSE\"\n" +
                    "          }\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"range\": {\n" +
                    "            \"@timestamp\": {\n" +
                    "              \"gte\": \""+format+":00:00\",\n" +
                    "              \"lte\": \""+format+":59:59\"\n" +
                    "            }\n" +
                    "          }\n" +
                    "        }\n" +
                    "        ]\n" +
                    "    }\n" +
                    "  },\n" +
                    "  \"aggs\": {\n" +
                    "    \"group_by_arr\": {\n" +
                    "      \"terms\": {\n" +
                    "        \"script\": \"doc['ARR.keyword'].value+'#'+doc['DEP.keyword'].value\",\n" +
                    "        \"size\": 10000\n" +
                    "      }\n" +
                    "    }\n" +
                    "  }\n" +
                    "}");
            //请求
            HttpResponse execute = post.execute();
            //获得响应结果
            String json = execute.body();
//            System.out.println(json);
            //转成map
            Map map = JSONUtil.toBean(json, Map.class);
            //取出其中的航线信息
            List<Map<?,?>> list = (List<Map<?,?>>)((Map)((Map)map.get("aggregations")).get("group_by_arr")).get("buckets");
            //遍历每个航线
            for(Map<?,?> row : list){
                //从总的结果中得到这个航线的查询次数
                Integer value = countMap.get(row.get("key") + "");
                //为空置0
                if(value == null){
                    value = 0;
                }
                //和之前统计的累加
                value+=Integer.parseInt(row.get("doc_count")+"");
                //放到总的结果中
                countMap.put(row.get("key") + "",value);
            }
        }
        return countMap;
    }
}
